/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import control.Controller;
import control.Main;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import models.Var;

/**
 *
 * @author Béo
 */
public class viewCleaning extends GameCanvas {

    private Graphics g;
    private Image load, successful;
    private Sprite sprLoad;
    private LayerManager manager = new LayerManager();
    private viewCleaner viewcleaner;
    private Random rd;

    public viewCleaning() {
        super(false);
        g = getGraphics();
        try {
            load = Image.createImage(Var.LOAD);
            successful = Image.createImage(Var.SUCCESS);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        sprLoad = new Sprite(load, 18, 18);
        sprLoad.setFrameSequence(new int[]{0, 1, 2, 3});
        manager.append(sprLoad);
        sprLoad.setPosition(getWidth() / 2 - 9, getHeight() / 2);
    }

    public void drawBackground() {
        g.setColor(Var.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        flushGraphics();
    }

    public void draw() {
        String[] scanning = {"Scanning   ", "Scanning.  ", "Scanning.. ", "Scanning..."};
        String[] cleaning = {"Cleaning   ", "Cleaning.  ", "Cleaning.. ", "Cleaning..."};
        for (int i = 0; i < 30; i++) {
            drawBackground();
            manager.paint(g, 0, 0);
            g.setColor(Var.WHITE);
            if (i < 10) {
                g.drawString(scanning[i % 4], getWidth() / 2, getHeight() / 2 + 70, Graphics.HCENTER | Graphics.BASELINE);
            } else if (i >= 10) {
                g.drawString(cleaning[i % 4], getWidth() / 2, getHeight() / 2 + 70, Graphics.HCENTER | Graphics.BASELINE);
            }
            sprLoad.nextFrame();
            flushGraphics();
            try {
                Thread.sleep(170);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        drawBackground();
        g.drawImage(successful, 0, getHeight() / 2 - 20, Graphics.LEFT | Graphics.TOP);
        flushGraphics();
        Controller.viewcleaner.setFreeRam(Controller.viewcleaner.getFreeRam() + 1553);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        Controller.getInstance().showHome();
    }

    public void start() {
        Thread t = new Thread() {
            public void run() {
                draw();
            }
        };
        t.start();
    }
}
